import { Component } from 'react'
import Taro from '@tarojs/taro'
import { View, Text } from '@tarojs/components'
import './index.scss'
import global from '../../global';
import { Textarea, Button, Map, Swiper, SwiperItem, Picker, Input, Image } from '@tarojs/components'
import Request from "../../api";
import Event from "../../event";
export default class Index extends Component {

  constructor(props) {
    super(props);
    this.state = {
      form: {
        createBy: global.userInfo.userName ||'',
        userId: global.userInfo.userId || '',
        content: ''
      },
      loading: false
    };
    this.submitForm = this.submitForm.bind(this)
  }

  componentWillMount () { }

  componentWillUnmount () { }

  componentDidHide () { }

  async submitForm() {
    if (!this.state.form.content) {
      await Taro.showToast({
        icon: 'none',
        title: '请输入反馈内容！',
        duration: 1500
      });
      return;
    }
    this.state.loading = true;
    await this.setState({
      loading: true
    });
    try {
      console.log(this.state.form)

      let res = await Request.post('/wx/feedback', this.state.form);
      if (res.data.code !== 200) {
        throw new Error()
      }
      await Taro.navigateBack();
      await Taro.showToast({
        icon: 'success',
        title: '提交成功！',
        duration: 1500
      })
    } catch (e) {
      await Taro.showToast({
        icon: 'none',
        title: '提交失败,请重试!',
        duration: 1500
      })
    } finally {
      this.state.loading = false;
      await this.setState({
        loading: false
      });
    }

  }

  render () {

    return (
      <View className='feedback'>
        <Textarea style={{height: '250px'}} placeholder='对我们的平台您还有什么意见么？请告诉我们'
                  onInput={e => {
                    this.setState({
                      form: {
                        ...this.state.form,
                        content: e.detail.value
                      }
                    });
                    console.log(e)
                  }}/>
        <Button loading={this.state.loading} className='submit_btn' type='primary' onClick={this.submitForm}>提交</Button>
      </View>
    )
  }
}
